<template>
  <div id="rptResultBac">
    <vxe-toolbar class-name="bg-grey light">
      <template #buttons>
        <vxe-button type="text" class="button-blue margin-left-xs" content="打印选中" @click="printSelected"></vxe-button>
        <vxe-button type="text" class="button-blue margin-left-xs" content="打印全部" @click="printAll"></vxe-button>
      </template>
    </vxe-toolbar>
    <Splitpanes horizontal class="bacRptResult-splitpanes" :dbl-click-splitter="false">
      <Pane :size="paneTopSize">
        <Splitpanes>
          <Pane size="20">
            <el-scrollbar>
              <el-tree ref="rptTreeRef" node-key="id" :data="dataTree" :props="defaultProps" highlight-current :default-expand-all="true" @node-click="handleNodeClick" />
            </el-scrollbar>
          </Pane>
          <Pane size="80">
            <el-scrollbar>
              <div style="white-space: normal !important;width:99%;height:100%;">
                <ActiveReportViewer :reportName="rptId" :reportParamter="rptParams" :isShowParamPanel="isSHowParamPanel" />
              </div>
            </el-scrollbar>
          </Pane>
        </Splitpanes>
      </Pane>
      <Pane :size="paneBottomSize">
        <BacHistories ref="BacHistoriesRef" />
      </Pane>
    </Splitpanes>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, getCurrentInstance, defineProps, onMounted } from 'vue';
import { Splitpanes, Pane } from 'splitpanes';
import { formatDateTime, IsEmpty } from '@/utils/common';
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { VXETable, VxeFormPropTypes, VxeFormInstance } from 'vxe-table';
import { AppStore } from '@/stores/store';
import { SysOptions } from '@/stores/sysoptions';
import BacHistories from './BacHistories.vue';
import ActiveReportViewer from '@/components/ActiveReportViewer.vue';


defineExpose({
  initPage
});
let viewerRptShowRef = ref(null);/**报表对象 */
const $this = {} as any;
const storeObj = AppStore();
const sysoptionObj = SysOptions(); //系统参数状态管理对象
let { proxy }: any = getCurrentInstance();
const BacHistoriesRef = ref(null); /**历史结果组件对象 */
let labMainInfoObj: any = reactive({}); /**检验记录当前选中对象 */
let rptResultBac: any[] = reactive([]); /**细菌检测结果列表 */
let paneTopSize = ref(78);
let paneBottomSize = ref(22);
let rptOverviewListObj = reactive({});/* 报告列表集合对象 */
const defaultProps = { children: 'children', label: 'title', }
let dataTree: Tree[] = reactive([]);
const rptId = ref('');
const rptParams: any = reactive({});
const isSHowParamPanel = ref(false);
const rptTreeRef = ref(null);/* 树形组件对象 */
// [
//   {
//     reportTemplateName: '一级报告',
//     children: [],
//   },
//   {
//     reportTemplateName: '二级报告',
//     children: [],
//   },
//   {
//     reportTemplateName: '细菌药敏报告',
//     children: [],
//   },
// ];
//#region  计算属性

//#endregion

//#region  生命周期函数
onMounted(() => {
  //todo
});
//#endregion

/**初始化界面 */
function initPage(mainInfoRow: any) {
  Object.assign(labMainInfoObj, mainInfoRow);
  findBacRptOverview();
  //findRptResultBac();
  getBacHistories();
  //getInstrGermResultInit();
}

//历史回顾传值
function getBacHistories() {
  BacHistoriesRef.value?.initPage(labMainInfoObj);
}

/* 报告查询列表 */
function findBacRptOverview() {
  if (IsEmpty(labMainInfoObj.rptGroup) || IsEmpty(labMainInfoObj.sampleda) || IsEmpty(labMainInfoObj.sampleNo)) {
    return;
  }

  const url = '/lab/maininfoBac/findBacReportList';
  let data = { rptGroup: labMainInfoObj.rptGroup, sampleda: labMainInfoObj.sampleda + ' 00:00:00', sampleNo: labMainInfoObj.sampleNo } as any;
  proxy.$http
    .get(url, { params: data })
    .then((res: any) => {
      dataTree.length = 0;
      if (res && res.data) {
        Object.assign(rptOverviewListObj, res.data);
        if (res.data.firstRptList && res.data.firstRptList.length > 0) {
          let rpt1 = { title: '一级报告', children: res.data.firstRptList };
          dataTree.push(rpt1);
        }
        if (res.data.secRptList && res.data.secRptList.length > 0) {
          let rpt2 = { title: '二级报告', children: [...res.data.secRptList] };
          dataTree.push(rpt2);
        }
        if (res.data.finalRptList && res.data.finalRptList.length > 0) {
          let rpt3 = { title: '细菌药敏报告', children: res.data.finalRptList };
          dataTree.push(rpt3);
        }
        if (dataTree && dataTree.length > 0) {
          rptTreeRef.value!.setCurrentNode(dataTree[0].children[0], true);
        }
      }
    })
    .catch((err: any) => { });
}

/* 树形节点选中 */
function handleNodeClick(data: Tree) {
  rptId.value = data.reportId;
  Object.assign(rptParams, { reportId: data.reportId, reportParams: [{ Name: 'Id', Values: [data.id] }] });
  /* 打印选中 */
}
/* 打印选中 */
function printSelected() {
  const selNode = rptTreeRef.value!.getCurrentNode();
  if (!selNode || selNode.length <= 0) {
    VxeMessage('请选中一条报告记录', VxeNotifyType.warning);
    return;
  }
  const item = { ReportName: selNode.reportId, ReportParameters: [{ Name: 'Id', Value: selNode.id }], PdfPrinterSetting: { PdfPrinterCopies: 1 } };
  const arr = [item];
  proxy.$emit('confirmRptPrint', arr);
}
/* 全部打印 */
function printAll() {
  const arr = [];
  if (rptOverviewListObj.firstRptList && rptOverviewListObj.firstRptList.length > 0) {
    rptOverviewListObj.firstRptList.forEach((item: any) => {
      const itm = { ReportName: item.reportId, ReportParameters: [{ Name: 'Id', Value: item.id }], PdfPrinterSetting: { PdfPrinterCopies: 1 } };
      arr.push(itm);
    });
  }
  if (rptOverviewListObj.secRptList && rptOverviewListObj.secRptList.length > 0) {
    rptOverviewListObj.secRptList.forEach((item: any) => {
      const itm = { ReportName: item.reportId, ReportParameters: [{ Name: 'Id', Value: item.id }], PdfPrinterSetting: { PdfPrinterCopies: 1 } };
      arr.push(itm);
    });
  }
  if (rptOverviewListObj.finalRptList && rptOverviewListObj.finalRptList.length > 0) {
    rptOverviewListObj.finalRptList.forEach((item: any) => {
      const itm = { ReportName: item.reportId, ReportParameters: [{ Name: 'Id', Value: item.id }], PdfPrinterSetting: { PdfPrinterCopies: 1 } };
      arr.push(itm);
    });
  }
  if (!arr || arr.length <= 0) {
    VxeMessage('没有要打印的报告', VxeNotifyType.warning);
    return;
  }
  proxy.$emit('confirmRptPrint', arr);
}
</script>

<style scoped lang="scss">
@import '@/assets/css/el-tabs.scss';

#rptResultBac {
  height: calc(100%);
}

.bacRptResult-splitpanes {
  height: calc(100vh - 240px);
}

.lab-bac-test-result-tab {
  :deep(.el-tabs__header) {
    margin-bottom: 0;
  }

  :deep(.el-tabs__nav-prev) {
    line-height: 28px !important;
  }

  :deep(.el-tabs__nav-next) {
    line-height: 28px !important;
  }

  :deep(.el-tabs__content) {
    height: 0px;
    padding: 2px;
  }

  :deep(.el-tab-pane) {
    height: 0px;
  }
}

:deep(.cell-bgColor) {
  .vxe-cell {
    height: 100%;
  }
}

:deep(.bac-card) {
  cursor: pointer;

  .el-card__header {
    padding: 5px 5px;
  }

  .el-card__body {
    padding: 5px 5px;

    .bac-result-content {
      font-size: 0.8em;

      .label-des {
        font-weight: 550;
        margin-right: 2px;
      }
    }
  }

  .el-card__footer {
    padding: 5px 5px;
  }
}

.preserve-format {
  white-space: pre-wrap; /* 保留空格和换行，并支持自动换行 */
  font-family: monospace; /* 可选：使用等宽字体 */
}

/* 医院检测报告卡片样式 */
.report-card {
  width: 100%;
  max-width: 150px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  padding: 10px;
  font-family: Arial, sans-serif;
  color: #333;
  border-left: 6px solid #2e86c1;
  cursor: pointer;
  position: relative;
}

.report-card .check-box {
  position: absolute;
  top: 10px;
}

.report-card h2 {
  font-size: 20px;
  color: #2e86c1;
  margin-bottom: 10px;
}

.report-card .patient-info {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
  border-bottom: 1px solid #ddd;
  padding-bottom: 10px;
}

.report-card .test-result {
  font-size: 16px;
  font-weight: bold;
  color: #d35400;
  margin: 10px 0;
}

.report-card .status {
  font-size: 14px;
  padding: 2px 10px;
  border-radius: 6px;
  text-align: center;
  color: #fff;
  background: #27ae60;
  width: fit-content;
  margin-top: 2px;
}

.report-card .status.critical {
  background: #c0392b;
}

.report-card .footer {
  font-size: 12px;
  color: #999;
  margin-top: 15px;
  text-align: right;
}
</style>
